package FlowGram.Application.Parser.Actions.Control;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import FlowGram.Application.Parser.FinchParser;
import FlowGram.Application.Parser.FinchParserException;
import FlowGram.Application.Parser.Controllers.FinchController;


/**
 * 
 * @author thackray
 *
 */
public class Comment extends ControlAction {

	public enum CommentType{
		SINGLE_LINE,
		MULTI_LINE
	}
	
	
	protected String commentText;
	protected CommentType type;
	
	public String getCommentText() {
		return commentText;
	}

	public void setCommentText(String commentText) {
		this.commentText = commentText;
	}

	public CommentType getType() {
		return type;
	}

	public void setType(CommentType type) {
		this.type = type;
	}

	public Comment(FinchParser parser) {
		super(parser);
	}

	public Comment() {
		super();
	}
	
	public static boolean matches(String s){
		return s.matches("^[//|/\\*].+");
	}
	public static boolean matchsEnd(String s){
		return s.matches("^\\*/$");
	}
	public static String syntax(){
		return "// <comments> OR /* <comments> */";
	}
	@Override
	public void execute(FinchController controller) {
		//do nothing
	}

	@Override
	public String logMessage() {
		return "comment: "+this.commentText;
	}

	@Override
	public void parse(String statement) throws FinchParserException {
		Pattern p = Pattern.compile("^(//)(.*)$|^(/\\*)(.*?)$");
		Matcher matcher = p.matcher(statement);
		while(matcher.find()){
			if(matcher.group(1) != null){
				this.type = CommentType.SINGLE_LINE;
				this.commentText = matcher.group(2);
				
			}else{
				this.type = CommentType.MULTI_LINE;
				String buffer = "";
				String line = this.parser.getNextStatement();
				while(!matchsEnd(line)){
					buffer += line+"\n";
					line = this.parser.getNextStatement();
				}
				this.commentText = buffer;
			}	
		}
	}

	@Override
	public String toCode(int indent) {
		if(this.type == CommentType.SINGLE_LINE){
			return "//"+this.commentText;
		}else{
			return "/*"+this.commentText+"*/";
		}
	}
	@Override
	public String toHTML(String idPrefix){
		return this.toCode();
	}

}
